﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WebPeajes.VehiculosWS;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;

namespace WebPeajes.Paginas
{
    public partial class RegistrarCobro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string placa = Request.QueryString["placa"];
                

                VehiculosClient proxy = new VehiculosClient();
                Vehiculo vehiculo = new Vehiculo();
                vehiculo = proxy.obtenerVehiculo(int.Parse(placa));

                txtPlaca.Text = vehiculo.Placa.ToString();
                txtMarca.Text = vehiculo.Marca;
                txtModelo.Text = vehiculo.Modelo;
                txtColor.Text = vehiculo.Color;
                txtAnio.Text = vehiculo.Anio.ToString();

                txtRUC.Text = vehiculo.Empresa.RUC;
                txtRazonSocial.Text = vehiculo.Empresa.RazonSocial;
                txtTipo.Text = vehiculo.Tipo;

                //Cálculo Cobro por Eje
                txtEjes.Text = vehiculo.Eje.ToString();
                Vehiculo vehiculo2 = null;
                vehiculo2 = proxy.calcularCobroXEje(int.Parse(txtEjes.Text), txtTipo.Text);
                
                txtPrecioEje.Text = vehiculo2.Precio.ToString();
                txtTotalPeaje.Text = vehiculo2.Total.ToString();

                //Cálculo de la Detracción
                if (txtTipo.Text == "Liviano")
                {
                    ddlDetraccion.SelectedIndex = 2;
                    ddlDetraccion.Enabled = false;
                    BtnEvacion.Enabled = false;

                    Vehiculo vehiculo3 = null;
                    vehiculo3 = proxy.calcularDetraccion(int.Parse(txtEjes.Text));

                    txtEjeAfecto.Text = txtEjes.Text;
                    txtPrecioEjeAfecto.Text = vehiculo3.Precio2.ToString();
                    txtTotalDetraccion.Text = vehiculo3.Total.ToString();

                    txtTotal.Text = Convert.ToString(int.Parse(txtTotalPeaje.Text) + int.Parse(txtTotalDetraccion.Text));
                }
                //else if (txtTipo.Text == "Pesado")
                //{
                //    BtnEvacion.Enabled = false;
                //}                
            }
        }



        protected void BtnEvacion_Click(object sender, EventArgs e)
        {
            Response.Redirect("Detraccion.aspx");
        }

        protected void ddlDetraccion_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                    if (ddlDetraccion.SelectedIndex == 1)
                    {
                        BtnEvacion.Enabled = true;

                        VehiculosClient proxy = new VehiculosClient();
                        Vehiculo vehiculo3 = null;
                        vehiculo3 = proxy.calcularDetraccion(int.Parse(txtEjes.Text));

                        txtEjeAfecto.Text = txtEjes.Text;
                        txtPrecioEjeAfecto.Text = vehiculo3.Precio2.ToString();
                        txtTotalDetraccion.Text = vehiculo3.Total2.ToString();
          
                    }
                    else if (ddlDetraccion.SelectedIndex == 2)
                    {
                        BtnEvacion.Enabled = true;

                        txtEjeAfecto.Text = txtEjes.Text;
                        txtPrecioEjeAfecto.Text = "0";
                        txtTotalDetraccion.Text = "0";
                    }
                    txtTotal.Text = Convert.ToString(int.Parse(txtTotalPeaje.Text) + int.Parse(txtTotalDetraccion.Text));
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void btnPagar_Click(object sender, EventArgs e)
        {
            string usuario = Session["usuario"].ToString();
            // Prueba de obtención de empleado vía HTTP GET
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create("http://localhost:17279/Empleados.svc/Usuarios?usuario=" + usuario);
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string empleadoJson2 = reader2.ReadToEnd();
            JavaScriptSerializer js2 = new JavaScriptSerializer();
            Empleado empleadoObtenido = js2.Deserialize<Empleado>(empleadoJson2);



            WSCobros.CobrosClient wsCobros = new WSCobros.CobrosClient();
            WSCobros.Ticket ticket = null;
            ticket = wsCobros.generarTicket(DateTime.Today.Date, empleadoObtenido.Codigo);

            //Registrar cobro de peaje
            WSCobros.VehiculoTicket cobroNuevo = null;
            cobroNuevo = wsCobros.registrarCobro(int.Parse(txtPlaca.Text), ticket.Numero, int.Parse(txtTotalPeaje.Text), int.Parse( txtTotalDetraccion.Text), 0);

            Limpiar();
            lblMensaje.Text = "Se registró satisfactoriamente";
        }

        private void Limpiar()
        {
            txtPlaca.Text = "";
            txtMarca.Text = "";
            txtModelo.Text = "";
            txtColor.Text = "";
            txtAnio.Text = "";
            txtRUC.Text = "";
            txtRazonSocial.Text = "";
            txtTipo.Text = "";
            txtEjes.Text = "";
            txtPrecioEje.Text = "";
            txtTotalPeaje.Text = "";
            txtTotal.Text = "";
            txtEjeAfecto.Text = "";            
            txtPrecioEjeAfecto.Text = "";
            txtTotalDetraccion.Text = "";
            txtTotal.Text = "";
            lblMensaje.Text = "";
        }
    }
}